package com.zlt.bkparking.quart;

import com.zlt.bkparking.entity.Advertisement;
import com.zlt.bkparking.service.AdvertisementService;
import com.zlt.bkparking.utils.ApplicationContextUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Date: 2022/10/12 9:09
 * @Author: hzj
 * @Description:
 */
@Component
public class GetAdByDay implements Job {



    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        RedisTemplate redisTemplate= (RedisTemplate) ApplicationContextUtil.getBean("redisTemplate");
        AdvertisementService advertisementService= (AdvertisementService) ApplicationContextUtil.getBean("advertisementService");
        List<Advertisement> advertisements = advertisementService.getAdvertisementByToday();
        advertisements.stream().forEach(a->{
            redisTemplate.opsForValue().set("advertisement:id:"+a.getAdId(),a.getAdPath(),1, TimeUnit.DAYS);
            //TODO redis保存每天播放次数
            redisTemplate.opsForValue().set("advertisement:num:"+a.getAdId(),a.getAdDayCount(),1, TimeUnit.DAYS);
        });

    }
}
